# geometry.two_d.circle -- Module for circle geometry
#
# Copyright (c) 2000, V.Kelson and M.Bakker
#

import copy
import math

import geometry.xy
import base
import point
import rectangle

class Circle(base.Geometry2d):
	'''Class for Circle Geometry

	Python call: Circle(xc,yc,R)

	Attributes:
	
	  - center: Point object of center of circle
	  - zc: complex coordinates of center of circle
	  - radius: radius of circle, float
	  - radsq; radius squared'''
	def __init__(self,xc,yc,radius=0.0):
		'''Constructor

		Python call: Circle(xc,yc,R). 
		xc and yc are center of circle, R is radius'''
		self.center = point.Point(xc,yc)
		self.zc = complex(self.center)
		self.radius = float(radius)
		self.radsq = self.radius * self.radius
	def __repr__(self):
		return 'Circle' + repr((self.center.x,self.center.y,self.radius))
	def __str__(self):
		return '[Circle at ' + str((self.center.x,self.center.y)) + \
		         ' radius= ' + str(self.radius) + ']'
	def isOnBoundary(self,pt,tol=0.0):
		tol = float(tol)
		r = math.sqrt(self.center.distance2(pt))
		if self.radius-tol <= r <= self.radius+tol:
			return 1
		else:
			return 0
	def isInArea(self,pt):
		return self.center.distance2(pt) <= self.radius*self.radius
	def circumCircle(self,tol=0.0):
		return Circle(self.center.x,self.center.y,self.radius+float(tol))
	def map(self,pt):
		return [ ( complex(pt) - self.zc ) / self.radius ]
	def distance2(self,pt):
		return self.center.distance2(pt) 
	def bbox(self,tol=0.0):
		rspace = geometry.xy.XY(self.radius,self.radius) + \
			 geometry.xy.XY(float(tol),float(tol))
		return rectangle.Rectangle(self.center-rspace,self.center+rspace)
	def area(self):
		'''Computes area of circle

		Returns the area of the circle as a float'''
		return math.pi * self.radius * self.radius
	def perimeter(self):
		'''Computes perimeter (circumference)

		Returns the circumference of the circle (2piR)'''
		return math.pi * 2.0 * self.radius
